package com.example.xianji.adapter;

import java.util.ArrayList;

import com.example.xianji.Contant;
import com.example.xianji.activity.Main_XuQiu_DetailsActivity;
import com.example.xianji.activity.Main_Zhanhui_DetailsActivity;
import com.example.xianji.R;
import com.example.xianji.bean.Home;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class HomeAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Home> home_list;
    private int come_from;

    public HomeAdapter(Context mContext, ArrayList<Home> home_list, int come_from) {
        super();
        this.mContext = mContext;
        this.home_list = home_list;
        this.come_from = come_from;
    }

    @Override
    public int getCount() {
        return home_list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder mHolder;
        if (convertView == null) {
            mHolder = new Holder();
            convertView = View.inflate(mContext, R.layout.home_listview_item, null);
            mHolder.textview_home_title = (TextView) convertView.findViewById(R.id.textview_home_title);
            mHolder.textview_home_place = (TextView) convertView.findViewById(R.id.textview_home_place);
            convertView.setTag(mHolder);
        }
        mHolder = (Holder) convertView.getTag();
        mHolder.textview_home_title.setText(home_list.get(position).getTitle());
        if (come_from == Contant.HomePage_Xuqiu_list) {
            //首页的需求列表
            if (home_list.get(position).getProvince_name().equals("null") && home_list.get(position).getCity_name().equals("null")) {
                mHolder.textview_home_place.setText(new StringBuffer("所在地:   ").append("").toString());
            } else {
                mHolder.textview_home_place.setText(new StringBuffer("所在地:   ").append(home_list.get(position).getProvince_name()).
                        append(home_list.get(position).getCity_name()).toString());
            }
        } else if (come_from == Contant.HomePage_Zhanhui_list) {
            //首页的展会列表
            mHolder.textview_home_place.setText(new StringBuffer("开展时间：     ").append(home_list.get(position).getTime()).toString());
        }
        final int a = position;
        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (come_from == Contant.HomePage_Xuqiu_list) {
                    //首页的需求列表
                    Intent intent = new Intent(mContext, Main_XuQiu_DetailsActivity.class);
                    intent.putExtra("xuqiu_id", home_list.get(a).getId());
                    mContext.startActivity(intent);
                } else if (come_from == Contant.HomePage_Zhanhui_list) {
                    //首页的展会列表
                    Intent intent = new Intent(mContext, Main_Zhanhui_DetailsActivity.class);
                    intent.putExtra("zhanhui_id", Integer.parseInt(home_list.get(a).getId()));
                    intent.putExtra("zhanhui_title", home_list.get(a).getTitle());
                    mContext.startActivity(intent);
                }
            }
        });
        return convertView;
    }

    class Holder {
        TextView textview_home_title;
        TextView textview_home_place;

    }
}
